//
//  CAForumCell.m
//  cascade-realm
//
//  Created by QSC on 4/6/24.
//

#import "CAForumCell.h"
@interface CAForumCell()

@property (nonatomic,strong)UIView * iconsView;
@property (nonatomic,strong)UIButton * joinBtn;
@property (nonatomic,strong)UILabel * titleLabel;
@end
@implementation CAForumCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUp];
        
    }
    
    return self;
}

- (void)setUsers:(NSArray *)users{
    _users = users;
    [self setIconViews];
}


- (void)setUp{
    
    UIView * centerView = [UIView new];
    
    [self.contentView addSubview:centerView];
    centerView.layer.cornerRadius = 8;
    centerView.backgroundColor = [UIColor whiteColor];
    centerView.layer.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.08].CGColor;
    centerView.layer.shadowRadius = 5.0;
    centerView.layer.shadowOpacity = 1;
    centerView.layer.shadowOffset = CGSizeMake(0, 5);
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView).inset(24);
        make.top.equalTo(self.contentView).offset(10);
        make.centerY.equalTo(self.contentView);
        make.height.mas_equalTo(113);
    }];
    
    UILabel *title = [UILabel LabelWithFont:Font(18) color:Hex(0x404040) text:@"Waterfall lovers gathering"];
    [centerView addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(centerView).offset(16);
        make.top.equalTo(centerView).offset(12);
    }];
    self.titleLabel = title;
    
    
    
    UIView * iconView = [UIView new];

    [centerView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(title);
        make.top.equalTo(title.mas_bottom).offset(16);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(35);
    }];
    self.iconsView = iconView;
    
    UILabel * enter = [UILabel LabelWithFont:Font(12) color:[[UIColor blackColor] colorWithAlphaComponent:0.3] text:@"enter"];
    [centerView addSubview:enter];
    [enter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(title);
        make.top.equalTo(iconView.mas_bottom).offset(10);
        
    }];
    
    UIImageView * arrow = [[UIImageView alloc] initWithImage:Img(@"arrow_right")];
    [centerView addSubview:arrow];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(enter);
        make.left.equalTo(enter.mas_right).offset(10);
    }];
    
    UIButton * joinBtn = [UIButton ButtonWithFont:BoldFont(12) color:[UIColor whiteColor] text:@"join in"];
    [joinBtn setBackgroundImage:[UIImage imageWithColor:[[UIColor blackColor] colorWithAlphaComponent:0.08]] forState:UIControlStateNormal];
    [joinBtn setBackgroundImage:[Tool getBGImage:Hex(0xB3E600) and:Hex(0xD4E600) directionType:1 option:CGSizeMake(60, 30)] forState:UIControlStateSelected];
  
    
    BOOL didJoin = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"DidJoinForum--%@",CAUserInfo.shared.userInfo.userId]];
    joinBtn.selected = !didJoin;
    
    joinBtn.layer.cornerRadius = 15;
    joinBtn.clipsToBounds = YES;
    self.joinBtn = joinBtn;
    [joinBtn addTarget:self action:@selector(joinClick) forControlEvents:UIControlEventTouchUpInside];
    
    [centerView addSubview:joinBtn];
    [joinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(centerView).offset(-16);
        make.bottom.equalTo(centerView).offset(-12);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
    }];
    
}

- (void)joinClick{

    if (self.joinBtn.selected) {
        [Tool ShowLoading];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [Tool HideLoading];
            [EasyTextView showText:@"join success"];
            self.joinBtn.selected = NO;
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"DidJoinForum--%@",CAUserInfo.shared.userInfo.userId]];
        });
        
    }else{
        [Tool ShowLoading];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [Tool HideLoading];
            [EasyTextView showText:@"exit success"];
            self.joinBtn.selected = YES;
      
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"DidJoinForum--%@",CAUserInfo.shared.userInfo.userId]];
        });
    }
}

- (void)setIconViews{
    [self.iconsView removeAllSubviews];
    CGFloat IconW = 32;
    
    UIButton * temp = [UIButton new];
    for (int i = 0; i< self.users.count; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        btn.clipsToBounds = YES;
        btn.layer.cornerRadius = 16;
        CAHomeUser * u = self.users[i];
        [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:u.avatar ] forState:UIControlStateNormal placeholderImage:Img(@"defult_head")];
        btn.userInteractionEnabled = NO;
        [self.iconsView addSubview:btn];
        if(i == self.users.count -  1 && i >=4){
            [btn setTitle:@"+5" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = Font(14);

        }
        
        if (i == 0) {
            btn.frame = CGRectMake(0, 0, IconW, IconW);
        }else{
            btn.frame = CGRectMake(temp.right - 5, 0, IconW, IconW);
        }
        temp = btn;
       
    }
    
}

- (void)setModel:(CADataModel *)model{
    _model = model;
    
    self.titleLabel.text = model.title;
    self.users = @[model.getUser];
    self.joinBtn.selected = true;
    self.joinBtn.enabled = false;
}

@end
